package com.example.house.vm

import com.example.common.view.vm.BaseViewModel
import com.example.house.api.HouseApiService
import com.example.house.entity.HouseEntityItem
import com.example.house.model.HouseRepository
import com.example.house.model.HouseResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HouseViewModel @Inject constructor(private val apiService: HouseApiService):BaseViewModel<HouseRepository,HouseResultData<List<HouseEntityItem>>>(){
    fun getListData(regionId:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getHouse(regionId)
                val res = HouseResultData(
                    data = result.data,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getListDataAll(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getHouseAll()
                val res = HouseResultData(
                    data = result.data,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}